package ch10_thread.thread;

/**
 * 对象锁形式-同步代码块
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class Synchronized03 implements Runnable {
    @Override
    public void run() {
        // this等价于声明一个锁对象
        synchronized (this) {
            System.out.println("对象锁的代码块，我叫：" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行结束");
        }
    }

    public static void main(String[] args) {
        Synchronized03 runnable = new Synchronized03();
        Thread t01 = new Thread(runnable);
        Thread t02 = new Thread(runnable);
        t01.start();
        t02.start();
    }
}
